//
//  TipAbstractWaterfallFlowCell.m
//  WaterfallFlowDemo
//
//  Created by Lining on 15/5/3.
//  Copyright (c) 2015年 Lining. All rights reserved.
//

#import "TipAbstractWaterfallFlowCell.h"
#import "WaterFlowDisplayInfo.h"
#import "UIImageView+QiNiu.h"
#import "TipInfoToolBar.h"
@interface TipAbstractWaterfallFlowCell ()
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIImageView *headImageView;
@property (strong, nonatomic) UIView *lineView;
@property (nonatomic , strong) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *descriptionLabel;
@end

@implementation TipAbstractWaterfallFlowCell
- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		[self addSubview:self.imageView];
		[self addSubview:self.headImageView];
		[self addSubview:self.lineView];
		[self addSubview:self.nameLabel];
		[self addSubview:self.descriptionLabel];
	}
	return self;
}

- (UIImageView *)imageView
{
	if (!_imageView)
	{
		_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
																  self.width, self.good.imageHeight)];
		_imageView.clipsToBounds = YES;
		_imageView.contentMode = UIViewContentModeScaleAspectFill;
	}
	return _imageView;
	
}

- (UILabel *)descriptionLabel
{
	if (!_descriptionLabel)
	{
		_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, self.height - 24, self.width - 24, self.good.titleHeight)];
		_descriptionLabel.font = self.good.titleFont;
		_descriptionLabel.numberOfLines = 0;
		_descriptionLabel.textColor = [UIColor colorWithHexString:@"878787"];
	}
	return _descriptionLabel;
	
}

- (void)setGood:(WaterFlowDisplayInfo *)good
{
    _good = good;
    NSURL *url = [NSURL URLWithString:good.img];
    [self.imageView setQNImageWithURL:url
                     placeholderImage:nil];
    self.descriptionLabel.text = good.price;
	
	self.nameLabel.text = good.abstract.memberNickName;
}

- (UIImageView *)headImageView
{
	if (!_headImageView)
	{
		_headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kUserHeaderSize, kUserHeaderSize)];
		_headImageView.layer.cornerRadius = _headImageView.width/2.0;
		[self addSubview:_headImageView];
	}
	return _headImageView;
}


- (UIView *)lineView
{
	if (!_lineView)
	{
		_lineView = [[UIView alloc] initWithFrame:CGRectMake(12, 0, self.width - 24, 0.5)];
		_lineView.backgroundColor = [UIColor grayColor];
		[self addSubview:_lineView];

	}
	return _lineView;
}

- (UILabel *)nameLabel
{
	if (!_nameLabel)
	{
		_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)];
		_nameLabel.font = [UIFont systemFontOfSize:18];
		_nameLabel.backgroundColor = [UIColor clearColor];
		_nameLabel.textColor = [UIColor darkGrayColor];

	}
	return _nameLabel;
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	self.imageView.frame = CGRectMake(0, 0, self.width, self.good.imageHeight);
	self.descriptionLabel.frame = CGRectMake(12,
                                             self.imageView.bottom + kControlInterval,
                                             self.width - 24,
                                             self.good.titleHeight);
	_descriptionLabel.font = self.good.titleFont;

	[self.descriptionLabel sizeToFit];
	if (self.descriptionLabel.width < self.width - 24) 
	{
		self.descriptionLabel.width = self.width - 24;
	}

	[self.headImageView setQNImageWithURL:[NSURL URLWithString:self.good.abstract.memberPhoto]
						  placeholderImage:[UIImage imageNamed:@"uv_default_avatar"]];
	

	if (!_toolBar)
	{
		_toolBar = [TipInfoToolBar tipInfoToolBarWithAbstractList:self.good.abstract
														  andSize:CGSizeMake(self.width, self.good.toolBarHeight)];
//		_toolBar.backgroundColor = [UIColor redColor];
		[self addSubview:_toolBar];
	}
	
	[self.toolBar layout];

	self.toolBar.top = self.descriptionLabel.bottom + 5;
	self.toolBar.userInteractionEnabled = NO;
	self.lineView.top = self.toolBar.bottom + kControlInterval;
	self.headImageView.left = 12;
	self.headImageView.top  = self.lineView.bottom + kControlInterval;
	
	
	self.nameLabel.centerY = self.headImageView.centerY;
	self.nameLabel.width = self.width - (self.headImageView.right + kControlInterval) - 12;
	self.nameLabel.left = self.headImageView.right + kControlInterval;

	
}
@end
